'''
Descripttion: 
Author: liubo
version: 
Date: 2021-12-20 14:13:00
LastEditors: liubo
LastEditTime: 2021-12-30 15:48:07
'''
import os
import shutil
import zipfile
d_pairchange = {"/*uselocal2pack":"/*uselocal2pack*/"
                ,"uselocal2pack*/":"uselocal2pack/*"
                ,"<!--uselocal2pack":"<!--uselocal2pack-->"
                ,"uselocal2pack-->":"uselocal2pack--><!--"}
def getAllFile(filepath):
    l_out = []
    # for fileName in os.listdir(filepath):  
    #     if os.path.splitext(fileName)[1] in ['.py'] :
    #         continue

    #     l_out.append(os.path.join(filepath,fileName))
    # l_use = ['.html','.']
    for root,dirs,files in os.walk(filepath): 

        for file in files: 
            l_out.append(os.path.join(root,file))
    return l_out
def main():
    l_usepath = ['js','images','css']
    l_usefile = ['index.html']
    l_useext = ['.html','.js','.css']
    nowpath = os.getcwd()
    outpath = os.path.join(nowpath,'out')
    # os.system("mkdir \"{0}\"".format(outpath))
    if not os.path.exists(outpath):
        os.mkdir(outpath)    

    for l in l_usepath:
        t_outpath = os.path.join(outpath,l)
        if not os.path.exists(t_outpath):
            os.mkdir(t_outpath)
        _files = getAllFile(os.path.join(nowpath,l))
        for f in _files:
            shutil.copy(f,t_outpath)
    for l in l_usefile:
        shutil.copy(os.path.join(nowpath,l),outpath)
    l_allfile = getAllFile(outpath)
    for filename in l_allfile:
        
        lines = []
        (t_path, tempfilename) = os.path.split(filename)
        (t_filename,t_ext) = os.path.splitext(tempfilename)
        if t_ext not in l_useext:
            continue
        with open(filename,'r',encoding='utf-8') as fin:
            
            lines = fin.readlines()
            
            for idx , line in enumerate(lines):
                if line.find('uselocal2pack')>=0:
                    struse = line.replace("\n",'').replace(' ','').replace('\t','').replace('\r','')
                    if struse in d_pairchange.keys():
                        lines[idx] = d_pairchange[struse] + '\n'
                    else:
                        print(line)
        with open(os.path.join(t_path,tempfilename),'w',encoding='utf-8') as fout:
            fout.writelines(lines)
    create_zip_file = zipfile.ZipFile(os.path.join(outpath,'player.datat'), mode='w', compression=zipfile.ZIP_STORED)
    for l in l_usepath:
        dirpath = os.path.join(outpath,l)
        for path,dirnames,filenames in os.walk(dirpath):
            # 去掉目标跟路径，只对目标文件夹下边的文件及文件夹进行压缩
            fpath = path.replace(dirpath,'')
            for filename in filenames:
                create_zip_file.write(os.path.join(path,filename),os.path.join(l,filename))
    print(111)
if __name__ == "__main__":
    main()